package leetcode.n2022;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;

/**
 * https://leetcode-cn.com/problems/elimination-game/
 * @program: brushing
 * @description:
 * @author: LiuQiang
 * @create: 2022-01-02
 **/
public class LastRemaining {


    public int lastRemaining(int n) {
        return lastRemaining(n, true);
    }

    public int lastRemaining(int n, boolean dir) {
        if (n == 1) {
            return 1;
        }

        return 2*lastRemaining(n/2, !dir) + (n%2 == 0 && !dir ? -1 : 0);
    }

    public static void main(String[] args) {
        Log log = LogFactory.get();
        LastRemaining lastRemaining = new LastRemaining();

        log.info(String.valueOf(lastRemaining.lastRemaining(6)));
    }
}
